package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.retail.entity.ViDeaRetailorder;
import com.vivo.retail.service.ViDeaRetailorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskViDeaRetailOrder {

    @Autowired
    private ViDeaRetailorderService viDeaRetailorderService;


    public void SyncData() {
        GetRetailOrder("ah");
        GetRetailOrder("js");
    }

    private void GetRetailOrder(String area) {
        System.out.println("get new retail data from " + area + " db start...");
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        ViDeaRetailorder lastItem = new ViDeaRetailorder();
        lastItem.setTableZone(area);
        lastItem = viDeaRetailorderService.getLast(lastItem);

        if (lastItem == null) {
            cal.set(2016, Calendar.JULY, 1);
            startDate = cal.getTime();
        } else {
            startDate = lastItem.getMakedate();
        }

        System.out.println("last change date is :" + startDate);

        int pageStart = 1;
        int pageSize = 5000;

        ViDeaRetailorder queryOrder = new ViDeaRetailorder();
        queryOrder.setBeginMakedate(startDate);
        queryOrder.setEndMakedate(endDate);
        Page<ViDeaRetailorder> pageParam = new Page<ViDeaRetailorder>(pageStart, pageSize);

        try {
            // 切换数据库
            Helper.changeDB(area);

            Page<ViDeaRetailorder> pageRes = viDeaRetailorderService.findPage(pageParam, queryOrder);
            DatabaseContextHolder.setMainDb();

            SaveRetailOrder(pageRes.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                System.out.println("total page is : " + pageRes.getTotalPage() + " cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());

                pageParam.setPageNo(i);

                // 切换数据库
                Helper.changeDB(area);

                pageRes = viDeaRetailorderService.findPage(pageParam, queryOrder);
                DatabaseContextHolder.setMainDb();

                SaveRetailOrder(pageRes.getList(), area);
            }
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get data from " + area + " db end...");
    }

    private void SaveRetailOrder(List<ViDeaRetailorder> list, String area) {
        List<ViDeaRetailorder> orderList = new ArrayList<ViDeaRetailorder>();


        for (ViDeaRetailorder order: list) {
            order.setIsTaskInsert(true);
            order.setId(IdGen.uuid());
            order.setTableZone(area);

            orderList.add(order);

            if (orderList.size() >= 1000) {
                // 保存单据
                saveOrders(orderList, area);

                orderList.clear();
            }
        }

        if (orderList.size() > 0) {
            // 保存单据
            saveOrders(orderList, area);
        }
    }


    private void saveOrders(List<ViDeaRetailorder> orderList, String area) {
        try {
            viDeaRetailorderService.insertByBatch(orderList, area);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 存在重复项目
            for (ViDeaRetailorder order: orderList) {
                try {
                    viDeaRetailorderService.save(order);
                } catch (Exception e) {
                    System.out.println("数据已经存在！ err:" + e.getMessage());
                }
            }
        }
    }
}
